(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:ClassesCtrl
     * @description
     * # ClassesCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('ClassesCtrl', ClassesCtrl);

    ClassesCtrl.$inject = ['$scope', 'dataService', 'helper', '$location', '$timeout', 'moment', '$rootScope'];

    function ClassesCtrl($scope, dataService, helper, $location, $timeout, moment, $rootScope) {

        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getCateList();
        }

        $scope.kclistAction = function() {
            $scope.class_category_id = $location.search().class_category_id;
            $scope.pageSize = 10;
            $scope.getList();
        }

        //根据用户搜索
        $scope.getmember = function() {

        }

        //获取列表
        $scope.getList = function(pageNo) {
            var query = {
                content: {
                    page_no: pageNo,
                    page_size: $scope.pageSize ? $scope.pageSize : 10,
                    store_id: $rootScope.store_id,
                    class_category_id: $scope.class_category_id,
                }
            };

            if ($scope.member_id) {
                query.content.member_id = $scope.member_id;
            }
            // query.content.left_num = "1";
            dataService.classGet(query).then(function(list) {
                if (list && list.class_list) {
                    $scope.class_list = list.class_list;
                    $scope.total = list.total_count;
                }
            });
        }

        //选择用户回调方法
        $scope.memberdata = function(memberInfo, t) {
            // console.error("memberInfo", memberInfo);
            $scope.member_id = memberInfo.member_id;
            if (t) {
                $scope.class.member_id = memberInfo.member_id;
            }
        }

        //订单详情
        $scope.getorder = function(id) {
            dataService.orderOne(id).then(function(one) {
                if (one) {
                    //sku显示
                    var items = one.items;
                    for (var i in items) {
                        var properties = items[i].sku_properties;
                        var a = properties.split(";");
                        var c = "";
                        for (var j in a) {
                            if (a[j]) {
                                var b = a[j].split(":");
                                var d = b[2] + ":" + b[3];
                                c = d + ";" + c;
                            }
                        }
                        items[i].sku_name = c;
                    }
                    one.items = items;
                    $scope.order = one;


                } else {
                    helper.myAlert("No data found", null, null, function() {
                        $state.go("order.list");
                    });
                }
            });
        }

        $scope.addAction = function() {


            $scope.order_id = $location.search().order_id;
            $scope.class_id = $location.search().class_id;
            $scope.class = {
                expiry_time: moment().add('days', 7).format('YYYY-MM-DD HH:mm:ss')
            };

            if ($scope.class_id) {
                getOneData();
                $scope.$on('event:up-store-id', function() {
                    getOneData();
                });
            }
            if ($scope.order_id) {
                $scope.getorder($scope.order_id);
            }
        }

        function getOneData() {
            var query = {
                content: {
                    class_id: $scope.class_id,
                    store_id: $rootScope.store_id,
                }
            };
            // console.error("$rootScope", $rootScope);
            dataService.classGet(query).then(function(result) {
                if (result.class_list) {
                    $scope.class = result.class_list[0];
                    $scope.class.left_num = Number($scope.class.left_num);
                    $scope.class.total_num = Number($scope.class.total_num);
                    // $scope.h5hd.content = $scope.h5hd.content ? json2html(JSON.parse($scope.h5hd.content)) : "";
                }

            });
        }

        $scope.categoryAction = function() {

            $scope.class_category_id = $location.search().class_category_id;

            if ($scope.class_category_id) {
                $scope.getCateList();

            } else {

            }
        }

        //获取列表
        $scope.getCateList = function(pageNo) {
            $scope.pageNo = pageNo ? pageNo : "1";

            var query = {
                content: {
                    page_no: $scope.pageNo,
                    page_size: $scope.pageSize ? $scope.pageSizes : 10,
                    store_id: $rootScope.store_id,
                }
            };

            if ($scope.class_category_id) {
                query.content.class_category_id = $scope.class_category_id;
            }

            if ($scope.name) {
                query.content.name = $scope.name;
            }

            dataService.classCategoryGet(query).then(function(list) {
                if (list) {
                    if ($scope.class_category_id) {
                        $scope.category = list.class_list[0];
                    } else {
                        $scope.class_list = list.class_list;
                        $scope.total_count = list.total_count;
                    }
                }
            });

            // dataService.itemGet(query).then(function(list) {
            //     if (list) {
            //         $scope.itemslist = list.items;
            //         $scope.itemsTotal = list.total_count;
            //     }
            // });
        }

        //课程类目添加
        $scope.cateAdd = function() {
            if (!$scope.category.name) {
                helper.myAlert("活动标题不能为空");
                return false;
            }
            var data = {
                content: {
                    "store_id": $rootScope.store_id,
                    "name": $scope.category.name,
                }
            }
            var ty = "add";
            if ($scope.class_category_id) {
                ty = "up";
                data.content.class_category_id = $scope.class_category_id;
            }

            var api = {
                "up": "classCategoryUpdate",
                "add": "classCategoryAdd"
            }

            dataService[api[ty]](data).then(function(result) {
                if (result) {
                    if (result.Msg) {
                        helper.myAlert("错误：" + result.Msg);
                        return false;
                    } else {
                        if (result.class_category_id) {
                            helper.myAlert("success", "#/classes/list");
                        } else {
                            helper.myAlert("错误：" + result);
                            return false;
                        }
                    }
                }
            });
        }

        //活动添加
        $scope.classAdd = function() {

            if ($scope.class_id || !$scope.order_id) {
                if (!$scope.class.total_num) {
                    helper.myAlert("课程总数不能为空");
                    return false;
                }

                if (!$scope.class.left_num) {
                    helper.myAlert("课程剩余数量不能为空");
                    return false;
                }

                if ($scope.class.left_num > $scope.class.total_num) {
                    helper.myAlert("课程剩余数量不能大于课程总数");
                    return false;
                }

                if (!$scope.class.expiry_time) {
                    helper.myAlert("过期时间不能为空");
                    return false;
                }

                if (!$scope.class.class_category_id) {
                    helper.myAlert("课程类目不能为空");
                    return false;
                }

                var data = {
                    content: {
                        "store_id": $rootScope.store_id,
                        "total_num": $scope.class.total_num,
                        "title": $scope.class.title,
                        "left_num": $scope.class.left_num,
                        "expiry_time": $scope.class.expiry_time,
                        "class_category_id": $scope.class.class_category_id,
                    }
                }

                if (!$scope.class_id) {

                    if (!$scope.class.member_id) {
                        helper.myAlert("没有选择上课用户不能为空");
                        return false;
                    }
                    data.content.member_id = $scope.class.member_id;
                } else {
                    data.content.class_id = $scope.class_id;
                    // data.content.left_num = $scope.class.left_num;
                }

                api(data).then(function(da) {
                    if (da) {
                        helper.myAlert("success", "#/classes/kclist?class_category_id=" + $scope.class.class_category_id);
                    }
                });
            } else {

                async.map($scope.order.items, function(da, callback) {
                    var data = {
                        content: {
                            "store_id": $rootScope.store_id,
                            "total_num": da.total_num,
                            "title": da.title,
                            "order_id": $scope.order_id,
                            "item_id": da.item_id,
                            "sku_id": da.sku_id,
                            "expiry_time": da.expiry_time,
                            "class_category_id": da.class_category_id,
                        }
                    }
                    api(data).then(function(da) {
                        if (da) {
                            callback(1, null);
                        } else {
                            callback(null, null);
                        }
                    });

                }, function(e, r) {
                    if (e) {
                        helper.myAlert("success", "#/classes/list");
                    }

                });
            }

            function api(data) {
                var method = $scope.class.class_id ? "classUpdate" : "classAdd";
                return dataService[method](data).then(function(result) {
                    if (result.Msg || !result.class_id) {
                        if (result.search("错误") != -1) {
                            helper.myAlert("一个订单只能创建一次课程！");
                        } else {
                            helper.myAlert("错误：" + result.Msg);
                        }
                        return false;
                    } else {
                        return true;
                        // helper.myAlert("success", "#/classes/list");
                    }
                });
            }
        }



    }
})();